/*
  File:    stopwatch.cpp
  Purpose: Various timer functions.

  Contact: Paul Macklin
           pmacklin@math.uci.edu
		  http://math.uci.edu/~pmacklin
*/

#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>

using namespace std;

#include "stopwatch.h"

time_t ProgramStartTime;
time_t ProgramEndTime;
time_t TicTime;
time_t TocTime;

void TIC(void)
{ TicTime = time(NULL); }
void TOC(void)
{ TocTime = time(NULL); }

void RUNTIME_TIC(void)
{ ProgramStartTime = time(NULL); }
void RUNTIME_TOC(void)
{ ProgramEndTime = time(NULL); }

double StopwatchValue(void)
{ return difftime(TocTime,TicTime); }

double RuntimeStopwatchValue(void)
{ return difftime( ProgramEndTime, ProgramStartTime ); }

void DisplayStopwatchValue( double dIn )
{
 int nDays = (int) floor( (double) (dIn / (60.0*60.0*24.0)) );
 int nHours = (int) floor( (double) ( (dIn - nDays*60*60*24) / (60.0*60.0)) );
 int nMinutes = (int) floor( (double) ( (dIn - nDays*60*60*24 - nHours*60*60 ) / (60.0)) );
 double dSeconds = dIn - nDays*60.0*60.0*24.0 - nHours * 60.0*60.0 - nMinutes * 60.0;

 cout << nDays << " days, " << nHours << " hours, " 
      << nMinutes << " minutes, and " << dSeconds << " seconds ";
}

void FormatStopwatchValue(char* szTextOut , double dIn)
{
 int nDays = (int) floor( (double) (dIn / (60.0*60.0*24.0)) );
 int nHours = (int) floor( (double) ( (dIn - nDays*60*60*24) / (60.0*60.0)) );
 int nMinutes = (int) floor( (double) ( (dIn - nDays*60*60*24 - nHours*60*60 ) / (60.0)) );
 double dSeconds = dIn - nDays*60.0*60.0*24.0 - nHours * 60.0*60.0 - nMinutes * 60.0;

 sprintf(szTextOut,
 "%d days, %d hours, %d minutes, and %2.4f seconds",
 nDays,nHours,nMinutes,dSeconds);
}

